//
//  SQRechangeOrderView.m
//  pai
//
//  Created by pro on 14-12-10.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQRechangeOrderView.h"
#import "UIImageView+WebCache.h"

@implementation SQRechangeOrderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

+(SQRechangeOrderView*)initmyview
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"SQRechangeOrderView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}
-(void)setItem:(order_t)order image:(NSString *)imageUrl
{
    if (order.type == 1) {
        self.typeLabel.text = @"竞拍商品";
    }else{
        self.typeLabel.text = @"兑换商品";
    }
    
    //图片
    [self.imageView setImageWithURL:[NSURL URLWithString:imageUrl]];
    //名称
    [self.nameLabel setText:[NSString stringWithUTF8String:order.skuname]];
    //市场价
    [self.priceLabel setText:@""];
    //我的出价
    NSString* pricestr = [NSString stringWithFormat:@"%d",order.price];
    NSString* pointsstr = [NSString stringWithFormat:@"%d",order.points];
    NSMutableAttributedString* price;
    if (order.price!=0&&order.points==0) {
        price = [[NSMutableAttributedString alloc]initWithString:[[NSString alloc] initWithFormat:@"使用了：%@豆",pricestr]];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,4)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4,pricestr.length)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(price.length-1,1)];
    }else if (order.price==0&&order.points!=0){
        price = [[NSMutableAttributedString alloc]initWithString:[[NSString alloc] initWithFormat:@"使用了：%@积分",pointsstr]];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,4)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4,pointsstr.length)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(price.length-2,2)];
    }else{
        price = [[NSMutableAttributedString alloc]initWithString:[[NSString alloc] initWithFormat:@"使用了：%@豆+%@积分",pricestr,pointsstr]];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,4)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4,pricestr.length)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(pricestr.length+4,2)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(pricestr.length+6,pointsstr.length)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(price.length-2,2)];
    }
    self.placeBidLabel.attributedText = price;
    NSLog(@"*************%d",order.order_addr.address_id);
    if (order.order_addr.address_id) {
        //有地址
        [self.addressButton setHidden:YES];
        //地址
        self.usernameLabel.text = [NSString stringWithUTF8String: order.order_addr.attn];
        self.mobileLabel.text = [NSString stringWithUTF8String: order.order_addr.phone];
        NSMutableString* addr =[NSMutableString stringWithUTF8String:order.order_addr.address];
        NSArray* arr = [addr componentsSeparatedByString:@"|"];
        if (arr.count == 1) {
            addr = arr[0];
        }else if(arr.count == 2){
            addr = [NSMutableString stringWithFormat:@"%@%@",arr[0],arr[1]];
        }
        self.addressLabel.text = [NSString stringWithFormat:@"%@%@%@",[NSString stringWithUTF8String:order.order_addr.province],[NSString stringWithUTF8String:order.order_addr.city],addr];
    }else{
        //无地址、选择地址
        [self.addressView setHidden:YES];
        [self.addressButton addTarget:self action:@selector(onActionSetAddress:) forControlEvents:UIControlEventTouchUpInside];
    }
}
-(void)onActionSetAddress:(UIButton*)btn
{
    NSLog(@"click address btn");
}
-(void)setAddress:(addr_t)address
{
    [self.addressView setHidden:NO];
    [self.addressButton setHidden:YES];
    self.nameLabel.text = [NSString stringWithUTF8String:address.attn];
    self.mobileLabel.text = [NSString stringWithUTF8String:address.phone];
    NSMutableString* addr =[NSMutableString stringWithUTF8String:address.address];
    NSArray* arr = [addr componentsSeparatedByString:@"|"];
    if (arr.count == 1) {
        addr = arr[0];
    }else if(arr.count == 2){
        addr = [NSMutableString stringWithFormat:@"%@%@",arr[0],arr[1]];
    }
    self.addressLabel.text = [NSString stringWithFormat:@"%@%@%@",[NSString stringWithUTF8String:address.province],[NSString stringWithUTF8String:address.city],addr];
}
@end
